package scheduler;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import core.DayOfWeek;
import core.ScheduleEntry;
import core.TimeOfTheClasses;
import core.WeekDefinition;
import commands.*;
import controllers.ScheduleController;

public class PairViewForm extends Form implements CommandListener{
	
	private StringItem m_pairNumItem;
	private StringItem m_timeItem;
	private StringItem m_subjNameItem;
	private StringItem m_subjTypeItem;
	private StringItem m_teachersNameItem; 
	private StringItem m_roomItem; 
	private StringItem m_comentsItem; 
	
	private static final Command OkCmd = new Command(Commands.Ok, Command.OK, 1);
	
	private static final String FormName = "Review pair"; 
	
	private WeekDefinition m_week;
    private DayOfWeek m_day;
    private TimeOfTheClasses m_time;
		
	public PairViewForm(){
		
		super(FormName);
		
		m_pairNumItem=new StringItem("Number: ", null);
		append(m_pairNumItem);
		m_timeItem=new StringItem("Time: ", null);
		append(m_timeItem);
		m_subjNameItem=new StringItem("Subject Name: ", null);
		append(m_subjNameItem);
		m_subjTypeItem=new StringItem("Subject Type: ", null);
		append(m_subjTypeItem);
		m_teachersNameItem=new StringItem("Teacher: ", null);
		append(m_teachersNameItem);
		m_roomItem=new StringItem("Room: ", null);
		append(m_roomItem);
		m_comentsItem=new StringItem("Comments: ", null);
		append(m_comentsItem);
		
		addCommand(OkCmd);
		
		setCommandListener(this);
		
		Dispatcher.getInstance().registerListener(this, Commands.DaySelected);
		Dispatcher.getInstance().registerListener(this, Commands.PairSelected);
		
	}

	
	public void commandAction(Command cmd, Displayable arg1) {
		switch(cmd.getCommandType()){
		case Command.OK :
			onOk();
			break;
		 case Command.ITEM:
			 if (cmd.getLabel().equals(Commands.DaySelected)){
                 DaySelectedCommand daySelectedCmd = (DaySelectedCommand)cmd;
                 daySelected(daySelectedCmd.getWeek(), daySelectedCmd.getDay());
			 }
             if (cmd.getLabel().equals(Commands.PairSelected)){
            	PairSelectedCommand pairSelectedCmd =  (PairSelectedCommand)cmd;
                pairSelected(pairSelectedCmd.getTime());
             }
             break;
		}
	}
	
	private void daySelected(WeekDefinition week, DayOfWeek day){
        m_week = week;
        m_day = day;
	}
	
	private void pairSelected(TimeOfTheClasses time){
		m_time = time;
		view();
	}
	
	public void view(){
		ScheduleEntry entry = ScheduleController.getInstance().getSchedule().getEntry(m_day, m_week, m_time);
		m_pairNumItem.setText(new Integer(entry.getTime().getTimeNumber()).toString());
		m_timeItem.setText(entry.getTime().getTimeDescription());
		m_subjNameItem.setText(entry.getSubject().getDiscipline().getName());
		m_subjTypeItem.setText(entry.getSubject().getDiscipline().getType().getTypeName());
		m_teachersNameItem.setText(entry.getSubject().getTeacher());
		m_roomItem.setText(entry.getSubject().getRoom());
		m_comentsItem.setText(entry.getSubject().getComment());
	}
		
	private void onOk()
	{
		Dispatcher.getInstance().handleCommand(new Command(Commands.PairViewOk, Command.ITEM,1));
	}
}
